package cn.pzhu.jspdb.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import cn.pzhu.jspdb.bean.User;

public class UserDao extends BaseDao {
	public List<User> listAll(){
		List<User> users=new ArrayList<User>();
		Connection conn=null;
		Statement st=null;
		ResultSet rs=null;
		try {
			conn=getConnection();
			st=conn.createStatement();
			rs=st.executeQuery("SELECT ID,LOGIN_NAME,PASSWORD FROM User ORDER BY ID ASC");
			while(rs.next()){
				User u=new User(rs.getInt("ID"), rs.getString("LOGIN_NAME"),
						rs.getString("PASSWORD"));
				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs,st,conn);
		}
		
		return users;
	}
	public List<User> listAll(int startIndex,int count){
		List<User> users=new ArrayList<User>();
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("SELECT ID,LOGIN_NAME,PASSWORD FROM User LIMIT ?,?");
			pst.setInt(1, startIndex);
			pst.setInt(2, count);
			
			rs=pst.executeQuery();
			while(rs.next()){
				User u=new User(rs.getInt("ID"), rs.getString("LOGIN_NAME"),
						rs.getString("PASSWORD"));
				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs,pst,conn);
		}
		
		return users;
	}
	
	public long countAll(){
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("SELECT COUNT(*) FROM User");
			rs=pst.executeQuery();
			if(rs.next()){
				return rs.getLong(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs,pst,conn);
		}
		return 0;
		
	}
	
	public boolean delById(int id){
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("DELETE FROM User WHERE ID=?");
			pst.setInt(1, id);
			flag=pst.executeUpdate()==1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close(pst,conn);
		}
		return flag;
	}
	
	public boolean add(User user){
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("INSERT INTO User(LOGIN_NAME,PASSWORD)VALUES(?,?)");
			pst.setString(1, user.getLoginName());
			pst.setString(2, user.getPassword());
			flag=pst.executeUpdate()==1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close(pst,conn);
		}
		return flag;
	}
	
	public User getUserById(int id){
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		User u=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("SELECT ID,LOGIN_NAME,PASSWORD FROM User WHERE ID=?");
			pst.setInt(1, id);
			rs=pst.executeQuery();
			if(rs.next()){
				 u=new User(rs.getInt("ID"), rs.getString("LOGIN_NAME"),
						rs.getString("PASSWORD"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs,pst,conn);
		}
		return u;
		
	}
	public User getUserByLoginName(String  loginName){
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		User u=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("SELECT ID,LOGIN_NAME,PASSWORD FROM User WHERE LOGIN_NAME=?");
			pst.setString(1, loginName);
			rs=pst.executeQuery();
			if(rs.next()){
				 u=new User(rs.getInt("ID"), rs.getString("LOGIN_NAME"),
						rs.getString("PASSWORD"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs,pst,conn);
		}
		return u;
		
	}
	
	public boolean update(int id,String loginName,String password){
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement("UPDATE User SET LOGIN_NAME=?,PASSWORD=? WHERE ID=?");
			pst.setString(1,loginName);
			pst.setString(2,password);
			pst.setInt(3, id);
			flag=pst.executeUpdate()==1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close(pst,conn);
		}
		return flag;
	}
	
}
